package com.zeny.express.framework.config;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Zookeeper框架工厂
 * 
 * @author zhuwei
 * @date 2013年8月30日 下午8:29:07
 */
@SuppressWarnings("unused")
public class CuratorFrameworkFactoryBean {

	/**
	 * 日志
	 */
	private static final Logger LOGGER = LoggerFactory
			.getLogger(CuratorFrameworkFactoryBean.class);

	/**
	 * 连接串
	 */
	private String url;

	/**
	 * TODO
	 */
	private RetryPolicy retryPolicy;
	private int sessionTimeoutMs;
	private int connectionTimeoutMs;

	/**
	 * Zookeeper框架
	 */
	private CuratorFramework curatorFramework;

	/**
	 * 初始化
	 * 
	 * @author zhuwei
	 * @date 2013年8月30日 下午8:32:30
	 */
	@PostConstruct
	public void init() {

		LOGGER.info("[framework] start init CuratorFrameworkFactoryBean: ");

		curatorFramework = CuratorFrameworkFactory.newClient(url,
				new ExponentialBackoffRetry(1000, 3));
		curatorFramework.start();
	}

	/**
	 * 销毁
	 * 
	 * @author zhuwei
	 * @date 2013年8月30日 下午8:32:39
	 */
	@PreDestroy
	public void destroy() {
		LOGGER.info("[framework] start destroy CuratorFrameworkFactoryBean: ");
		curatorFramework.close();
	}

	/**
	 * @return curatorFramework
	 */
	public CuratorFramework getCuratorFramework() {
		return curatorFramework;
	}

	/**
	 * @param url
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * @param retryPolicy
	 */
	public void setRetryPolicy(RetryPolicy retryPolicy) {
		this.retryPolicy = retryPolicy;
	}

	/**
	 * @param sessionTimeoutMs
	 */
	public void setSessionTimeoutMs(int sessionTimeoutMs) {
		this.sessionTimeoutMs = sessionTimeoutMs;
	}

	/**
	 * @param connectionTimeoutMs
	 */
	public void setConnectionTimeoutMs(int connectionTimeoutMs) {
		this.connectionTimeoutMs = connectionTimeoutMs;
	}

}
